﻿using System.Text.RegularExpressions;
using com.fukefu.source;

namespace com.fukefu.info.b2b.auto
{
    public class Bokee : Information
    {
        string uid = "", userId = "", login_name = "";
        int typdId = 0;
        //登录
        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData();
            pd.Add("username", name);
            pd.Add("password", pass);

            string html = source.Post("http://www.bokee.net/usermodule/userManager_quickLogin.do", pd.Read(), "http://www.bokee.net/");
            if (html.IndexOf("<li><a href=\"/usermodule/userManager_logout.do\">退出</a></li>") != -1)
            {
                this.login_name = name;
                uid = Regex.Match(html, "http://www\\.bokee\\.net/companymodule/company_indexCompany\\.do\\?id=([0-9]+)").Groups[1].Value;
                userId = Regex.Match(html, "userId = '([0-9]+)'").Groups[1].Value;
                return true;
            }
            return false;
        }
        //发布文章
        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            InformationResult result = new InformationResult();
            content = Regex.Replace(content, "<br.*?>", "");
            content = Regex.Replace(content, "<p>\\s*</p>", "", RegexOptions.IgnoreCase);
            if (typdId == 0)
            {
                result = PostGQ(title, content, "product");
            }
            else if (typdId == 1)
            {
                result = PostGQ(title, content, "supplying");
            }
            else if (typdId == 2)
            {
                result = PostGQ(title, content, "demanding");
            }
            else if (typdId == 3)
            {
                result = PostNewArticle(title, content);
            }
            else if (typdId == 4)
            {
                result = PostNews(title, content);
            }
            else
            {
                result.Code = 3;
            }
            if (result.Code == 0)
            {
                typdId++;
                return result;
            }
            return result;
        }
        //发布供求信息
        private InformationResult PostGQ(string title, string content, string type)
        {
            InformationResult result = new InformationResult();
            ParametersData pd = new ParametersData();
            pd.Add("total", "");
            pd.Add("pagerow", "");
            pd.Add("pagenum", "");
            pd.Add("id", "");
            pd.Add("selectedDirCode", site.TypeId[2]);
            pd.Add("companyId", uid);
            pd.Add("postMethod", type);
            pd.Add("title", title);
            pd.Add("rootDir", site.TypeId[0]);
            pd.Add("top_dir", site.TypeId[1]);
            pd.Add("middle_dir", site.TypeId[1] + "00001");
            pd.Add("_selectedDirCode", site.TypeId[2]);
            pd.Add("content", content);
            pd.Add("tags", "");
            pd.Add("seoTitle", title);
            pd.Add("seoKeywords", title);
            pd.Add("seoDescription", title);
            string html = source.Post("http://www.bokee.net/classificationmodule/biz/post_save.do", pd.Read(), "http://www.bokee.net/classificationmodule/biz/post_add.do?postMethod=product&companyId=" + uid);
            if (Regex.IsMatch(html, "<a href=\"[^\"]+\" target=\"_blank\">" + title + "</a>"))
            {
                result.Code = 1;
                result.Content = "http://www.bokee.net" + Regex.Match(html, "<a href=\"([^\"]+)\" target=\"_blank\">" + title + "</a>").Groups[1].Value;
            }
            return result;
        }
        //发布新闻
        private InformationResult PostNews(string title, string content)
        {
            InformationResult result = new InformationResult();
            ParametersData pd = new ParametersData();
            pd.Add("userId", userId);
            pd.Add("companyId", uid);
            pd.Add("title", title);
            pd.Add("source", "");
            pd.Add("content", content);
            pd.Add("notesubmit", "发布新闻");
            string html = source.Post("http://www.bokee.net/companymodule/note_saveNote.do", pd.Read(), "http://www.bokee.net/companymodule/note_addNote.do?id=" + uid + "&companyId=" + uid + "&storeId=" + uid);
            if (Regex.IsMatch(html, "<td><a href=\"/company/note_viewNote/[0-9]+\\.html\"\\s+target=\"_blank\">" + title + "</a></td>"))
            {
                result.Code = 1;
                result.Content = "http://www.bokee.net" + Regex.Match(html, "<td><a href=\"(/company/note_viewNote/[0-9]+\\.html)\"\\s+target=\"_blank\">" + title + "</a></td>").Groups[1].Value;
            }
            return result;
        }
        //发布文章
        private InformationResult PostNewArticle(string title, string content)
        {
            InformationResult result = new InformationResult();
            ParametersData pd = new ParametersData();
            pd.Add("bokeeName", "+login_name+");
            pd.Add("id", "0");
            pd.Add("goOnPost", "9");
            pd.Add("stateNo", "1");
            pd.Add("action", "updateEntry");
            pd.Add("title", title);
            pd.Add("content", content);
            pd.Add("brief", "");
            pd.Add("companyId", uid);
            pd.Add("relationCompanyId", uid);
            pd.Add("typeId", "-999");
            pd.Add("tags", "");
            pd.Add("visibility", "100");
            pd.Add("commentable", "1");
            pd.Add("showblogable", "10");
            pd.Add("columnName", "");
            string html = "";
            html = source.Post("http://www.bokee.net/companymodule/oneuser/weblog_save.do", pd.Read(), "http://www.bokee.net/companymodule/oneuser/weblog_save.do");
            if (Regex.IsMatch(html, "[0-9]+ 成功发表文章"))
            {
                result.Code = 1;
                result.Content = "http://www.bokee.net/company/weblog_viewEntry/" + Regex.Match(html, "([0-9]+) 成功发表文章").Groups[1].Value + ".html";
            }
            return result;
        }
    }
}
